package org.example.domain.plugin.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.plugin.service.BaseAsyncPlugin;
import org.example.types.model.*;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 简单Web搜索插件
 * 模拟从公开API或网站获取搜索结果
 */
@Slf4j
@Component
public class SimpleWebPlugin extends BaseAsyncPlugin {

    @Override
    public String getName() {
        return "simpleweb";
    }

    @Override
    public int getPriority() {
        return 3;
    }

    @Override
    protected Mono<PluginSearchResult> doSearch(String keyword, SearchContext context, Map<String, Object> ext) {
        log.info("SimpleWeb plugin searching for keyword: {}", keyword);

        return Mono.fromCallable(() -> createSearchResults(keyword))
                .map(this::createSuccessResult)
                .onErrorResume(ex -> {
                    log.warn("SimpleWeb plugin search failed for keyword: {}", keyword, ex);
                    return Mono.just(createErrorResult("搜索失败: " + ex.getMessage()));
                });
    }

    /**
     * 创建基于关键词的搜索结果
     * 这里模拟真实的搜索逻辑，实际可以调用真实API
     */
    private List<SearchResult> createSearchResults(String keyword) {
        List<SearchResult> results = new ArrayList<>();

        // 模拟不同类型的搜索结果
        if (keyword.contains("电影") || keyword.contains("影片")) {
            results.addAll(createMovieResults(keyword));
        }

        if (keyword.contains("软件") || keyword.contains("工具")) {
            results.addAll(createSoftwareResults(keyword));
        }

        if (keyword.contains("音乐") || keyword.contains("歌曲")) {
            results.addAll(createMusicResults(keyword));
        }

        // 如果没有特定分类，创建通用结果
        if (results.isEmpty()) {
            results.addAll(createGenericResults(keyword));
        }

        return results;
    }

    private List<SearchResult> createMovieResults(String keyword) {
        List<SearchResult> results = new ArrayList<>();

        // 百度网盘结果
        results.add(SearchResult.builder()
                .uniqueId("simpleweb-movie-" + keyword.hashCode())
                .title(keyword + " [HD 1080P]")
                .content("高清电影资源，支持在线播放")
                .datetime(LocalDateTime.now().minusDays(1))
                .links(List.of(
                        Link.builder()
                                .type("pan.baidu.com")
                                .url("https://pan.baidu.com/s/example123")
                                .password("2023")
                                .build()
                ))
                .build());

        // 阿里云盘结果
        results.add(SearchResult.builder()
                .uniqueId("simpleweb-movie2-" + keyword.hashCode())
                .title(keyword + " [4K 超清版]")
                .content("4K超高清电影，画质极佳")
                .datetime(LocalDateTime.now().minusHours(12))
                .links(List.of(
                        Link.builder()
                                .type("pan.aliyundrive.com")
                                .url("https://www.aliyundrive.com/s/example456")
                                .password("")
                                .build()
                ))
                .build());

        return results;
    }

    private List<SearchResult> createSoftwareResults(String keyword) {
        List<SearchResult> results = new ArrayList<>();

        results.add(SearchResult.builder()
                .uniqueId("simpleweb-soft-" + keyword.hashCode())
                .title(keyword + " 破解版")
                .content("包含激活工具和详细安装教程")
                .datetime(LocalDateTime.now().minusDays(2))
                .links(List.of(
                        Link.builder()
                                .type("pan.baidu.com")
                                .url("https://pan.baidu.com/s/software123")
                                .password("soft")
                                .build()
                ))
                .build());

        return results;
    }

    private List<SearchResult> createMusicResults(String keyword) {
        List<SearchResult> results = new ArrayList<>();

        results.add(SearchResult.builder()
                .uniqueId("simpleweb-music-" + keyword.hashCode())
                .title(keyword + " [无损音质]")
                .content("FLAC无损音质，音乐发烧友首选")
                .datetime(LocalDateTime.now().minusHours(6))
                .links(List.of(
                        Link.builder()
                                .type("pan.xunlei.com")
                                .url("https://pan.xunlei.com/s/music123")
                                .password("music")
                                .build()
                ))
                .build());

        return results;
    }

    private List<SearchResult> createGenericResults(String keyword) {
        List<SearchResult> results = new ArrayList<>();

        results.add(SearchResult.builder()
                .uniqueId("simpleweb-generic-" + keyword.hashCode())
                .title("「" + keyword + "」相关资源合集")
                .content("包含多个相关文件，资源丰富")
                .datetime(LocalDateTime.now().minusHours(3))
                .links(List.of(
                        Link.builder()
                                .type("pan.baidu.com")
                                .url("https://pan.baidu.com/s/generic123")
                                .password("0000")
                                .build()
                ))
                .build());

        return results;
    }

    @Override
    public String getDescription() {
        return "简单Web搜索插件，提供基础的网盘资源搜索";
    }
}